package com.zty.xiaomiadmin.server.Service.Good;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zty.xiaomiadmin.server.Dao.Good;
import com.zty.xiaomiadmin.server.Entity.Good.*;
import com.zty.xiaomiadmin.server.Entity.PageResult;
import com.zty.xiaomiadmin.server.Entity.QueryPageBean;
import com.zty.xiaomiadmin.server.utils.SqlSessionUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class GoodServiceImp implements GoodService {
    @Override
    public PageResult getGood(QueryPageBean queryPageBean) throws IOException {
        Integer currPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String name = queryPageBean.getName();
        int category = queryPageBean.getCategory();
        System.out.println(queryPageBean);
        PageHelper.startPage(currPage, pageSize);
        Map<String, Object> map = new HashMap<>();
        boolean nameFlag = false;
        boolean categoryFlag = false;
        if (name != null && !name.equals("")) {
            nameFlag = true;
            map.put("goodname", "%" + name + "%");
        } else {
            nameFlag = false;
        }
        if (category != 0) {
            categoryFlag = true;
            map.put("category", category);
        } else {
            categoryFlag = false;
        }
        map.put("currPage", (currPage - 1) * pageSize);
        map.put("pageSize", pageSize);
        Page<good> page;
        int total = 0;
        if (nameFlag && categoryFlag) {
            page = SqlSessionUtil.getSqlSessionInstance().getSqlSession().getMapper(Good.class).getGoodByCategoryAndName(map);
            total = SqlSessionUtil.getSqlSessionInstance().getSqlSession().getMapper(Good.class).getGoodByCategoryAndNameTotal(map);
        } else if (nameFlag) {
            page = SqlSessionUtil.getSqlSessionInstance().getSqlSession().getMapper(Good.class).getGoodsByName(map);
            total = SqlSessionUtil.getSqlSessionInstance().getSqlSession().getMapper(Good.class).getGoodsByNameTotal(map);
        } else if (categoryFlag) {
            page = SqlSessionUtil.getSqlSessionInstance().getSqlSession().getMapper(Good.class).getGoodByCategory(map);
            total = SqlSessionUtil.getSqlSessionInstance().getSqlSession().getMapper(Good.class).getGoodByCategoryTotal(map);
        } else {
            page = SqlSessionUtil.getSqlSessionInstance().getSqlSession().getMapper(Good.class).getGoods(map);
            total = SqlSessionUtil.getSqlSessionInstance().getSqlSession().getMapper(Good.class).getGoodsTotal();
        }
        List<good> list = page.getResult();
        return new PageResult(total, list);
    }

    @Override
    public GoodResult getGoodById(int category, int pageSize) throws IOException {

        GoodResult goodresult = new GoodResult();
        goodresult.setStatus(200);
        List<good> good = SqlSessionUtil.getSqlSessionInstance().getSqlSession().getMapper(Good.class).getGoodByCategoryString(category);
        goodresult.setList(good);
        int num =

                SqlSessionUtil.getSqlSessionInstance().getSqlSession().

                        getMapper(Good.class).getGoodPicCount();
        int currnum =

                SqlSessionUtil.getSqlSessionInstance().getSqlSession().

                        getMapper(Good.class).getGoodCurrCount(category);
        goodresult.setPageTotal(num);
        goodresult.setCurrTotal(currnum);

        return goodresult;
    }

    @Override
    public GoodResult getGoodByName(String goodname) throws IOException {
        GoodResult goodResult = new GoodResult();
        goodResult.setStatus(200);
        return goodResult;
    }

    @Override
    public void xjGood(good good) throws IOException {
        int finaid = SqlSessionUtil.getSqlSessionInstance().getSqlSession().
                getMapper(Good.class).getGoodPicCount();
        SqlSessionUtil.getSqlSessionInstance().getSqlSession().
                getMapper(Good.class).xjGood(good.getGood_id());
        SqlSessionUtil.getSqlSessionInstance().getSqlSession().commit();
        for (int i = good.getGood_id() + 1; i <= finaid; i++) {
            SqlSessionUtil.getSqlSessionInstance().getSqlSession().
                    getMapper(Good.class).updateGoodID(i, i - 1);
            SqlSessionUtil.getSqlSessionInstance().getSqlSession().
                    commit();
        }
    }

    @Override
    public void takeUpGood(good good) throws IOException {
        int finaid = SqlSessionUtil.getSqlSessionInstance().getSqlSession().
                getMapper(Good.class).getGoodPicCount();
        SqlSessionUtil.getSqlSessionInstance().getSqlSession().
                getMapper(Good.class).takeUpGood(good.getGood_id());
        SqlSessionUtil.getSqlSessionInstance().getSqlSession().commit();
        for (int i = good.getGood_id() + 1; i <= finaid; i++) {
            SqlSessionUtil.getSqlSessionInstance().getSqlSession().
                    getMapper(Good.class).updateGoodID(i, i - 1);
            SqlSessionUtil.getSqlSessionInstance().getSqlSession().
                    commit();
        }
    }

    @Override
    public void delGood(good good) throws IOException {

        int finaid =

                SqlSessionUtil.getSqlSessionInstance().getSqlSession().

                        getMapper(Good.class).getGoodPicCount();


        SqlSessionUtil.getSqlSessionInstance().getSqlSession().

                getMapper(Good.class).delGood(good.getGood_id());


        SqlSessionUtil.getSqlSessionInstance().getSqlSession().

                commit();

        for (int i = good.getGood_id() + 1; i <= finaid; i++) {


            SqlSessionUtil.getSqlSessionInstance().getSqlSession().

                    getMapper(Good.class).updateGoodID(i, i - 1);


            SqlSessionUtil.getSqlSessionInstance().getSqlSession().

                    commit();
        }


    }

    @Override
    public void delMulGood(List<good> goods) throws IOException {

        int finaid =

                SqlSessionUtil.getSqlSessionInstance().getSqlSession().

                        getMapper(Good.class).getGoodPicCount();

        int[] goodslen = new int[goods.size()];
        int i = 0;
        for (good good : goods) {
            goodslen[i++] = good.getGood_id();
        }

        int j = 0;
        int starid = goodslen[j];
        int delnum = 1;

        //删除第一个


        SqlSessionUtil.getSqlSessionInstance().getSqlSession().

                getMapper(Good.class).delGood(goodslen[j]);


        SqlSessionUtil.getSqlSessionInstance().getSqlSession().

                commit();

        for (i = starid + 1; i <= finaid; i++) {

            if ((j + 1 < goodslen.length) && i == goodslen[j + 1]) {


                SqlSessionUtil.getSqlSessionInstance().getSqlSession().

                        getMapper(Good.class).delGood(i);


                SqlSessionUtil.getSqlSessionInstance().getSqlSession().

                        commit();
                delnum++;
                j++;
            }


            SqlSessionUtil.getSqlSessionInstance().getSqlSession().

                    getMapper(Good.class).updateGoodID(i, i - delnum);


            SqlSessionUtil.getSqlSessionInstance().getSqlSession().

                    commit();
        }
    }

    @Override
    public GoodPushResult getGoodPush(good good) throws IOException {

        GoodPushResult goodPushResult = new GoodPushResult();
        good goodByName = SqlSessionUtil.getSqlSessionInstance().getSqlSession().
                            getMapper(Good.class).getGoodByName(good.getName().trim());
        if (goodByName != null) {
            //已存在商品
            goodPushResult.setStatus(400);
        } else {
            goodPushResult.setStatus(200);
            List<good> goods =SqlSessionUtil.getSqlSessionInstance().getSqlSession().
                                getMapper(Good.class).getGood();
            int finaid = goods.get(goods.size() - 1).getGood_id();
            int starid = finaid + 1;
            int goodidlast = SqlSessionUtil.getSqlSessionInstance().getSqlSession().
                                            getMapper(Good.class).getGoodPicCount();

            for (int i = goodidlast; i >= starid; i--) {
                SqlSessionUtil.getSqlSessionInstance().getSqlSession().
                        getMapper(Good.class).updateGoodID(i, i + 1);
                SqlSessionUtil.getSqlSessionInstance().getSqlSession().commit();
            }
            String createTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
            SqlSessionUtil.getSqlSessionInstance().getSqlSession().
            getMapper(Good.class).insertGood(starid, good.getName(), createTime, good.getCategory_id(),good.getPrice());
            SqlSessionUtil.getSqlSessionInstance().getSqlSession().commit();
            good newgoodbyname = SqlSessionUtil.getSqlSessionInstance().getSqlSession().
                                getMapper(Good.class).getGoodByName(good.getName());
            goodPushResult.setGood(newgoodbyname);
        }
        return goodPushResult;
    }

    @Override
    public GoodResult updGood(int goodid, String goodname,int price ,int category_id, int newhot) throws IOException {
        GoodResult goodResult = new GoodResult();

        if (goodname.equals("")) {
            goodResult.setStatus(404);
        } else {
            System.out.println(goodid);
            System.out.println(goodname);
            System.out.println(newhot);


            SqlSessionUtil.getSqlSessionInstance().getSqlSession().

                    getMapper(Good.class).updGood(goodid, goodname,price,category_id, newhot);


            SqlSessionUtil.getSqlSessionInstance().getSqlSession().

                    commit();
            goodResult.setStatus(200);
        }
        return goodResult;
    }

    @Override
    public PageResult getGoodPic(QueryPageBean queryPageBean) throws IOException {

        Integer currPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String name = queryPageBean.getName();
        int category = queryPageBean.getCategory();
        System.out.println(queryPageBean);
        PageHelper.startPage(currPage, pageSize);
        Map<String, Object> map = new HashMap<>();
        boolean nameFlag = false;
        boolean categoryFlag = false;
        if (name != null && !name.equals("")) {
            nameFlag = true;
            map.put("goodname", "%" + name + "%");
        } else {
            nameFlag = false;
        }
        if (category != 0) {
            categoryFlag = true;
            map.put("category", category);
        } else {
            categoryFlag = false;
        }
        map.put("currPage", (currPage - 1) * pageSize);
        map.put("pageSize", pageSize);
        Page<GoodPic> page;
        int total = 0;
        if (nameFlag && categoryFlag) {
            page = SqlSessionUtil.getSqlSessionInstance().getSqlSession().getMapper(Good.class).getGoodPicByCategoryAndName(map);
            total = SqlSessionUtil.getSqlSessionInstance().getSqlSession().getMapper(Good.class).getGoodByCategoryAndNameTotal(map);
        } else if (nameFlag) {
            page = SqlSessionUtil.getSqlSessionInstance().getSqlSession().getMapper(Good.class).getGoodsPicByName(map);
            total = SqlSessionUtil.getSqlSessionInstance().getSqlSession().getMapper(Good.class).getGoodsByNameTotal(map);
        } else if (categoryFlag) {
            page = SqlSessionUtil.getSqlSessionInstance().getSqlSession().getMapper(Good.class).getGoodPicByCategory(map);
            total = SqlSessionUtil.getSqlSessionInstance().getSqlSession().getMapper(Good.class).getGoodByCategoryTotal(map);
        } else {
            page = SqlSessionUtil.getSqlSessionInstance().getSqlSession().getMapper(Good.class).getGoodsPic(map);
            total = SqlSessionUtil.getSqlSessionInstance().getSqlSession().getMapper(Good.class).getGoodsTotal();
        }
        List<GoodPic> list = page.getResult();
        return new PageResult(total, list);
    }

    public GoodPicResult getGoodPicByName(String goodname) throws IOException {
        GoodPicResult goodPicResult = new GoodPicResult();
        goodPicResult.setStatus(200);

        List<GoodPic> goodPicByName = SqlSessionUtil.getSqlSessionInstance().getSqlSession().getMapper(Good.class).getGoodPicByName('%' + goodname + '%');
        int num = goodPicByName.size();
        int allnum = SqlSessionUtil.getSqlSessionInstance().getSqlSession().getMapper(Good.class).getGoodPicCount();
        goodPicResult.setPageTotal(allnum);
        goodPicResult.setCurrTotal(num);
        goodPicResult.setList(goodPicByName);
        return goodPicResult;
    }

    @Override
    public void updGoodPic(int id, String img, int kind) throws IOException {
        if (kind == 1) {
            SqlSessionUtil.getSqlSessionInstance().getSqlSession().getMapper(Good.class).setPic1(id, img);
            SqlSessionUtil.getSqlSessionInstance().getSqlSession().commit();
        } else if (kind == 2) {
            SqlSessionUtil.getSqlSessionInstance().getSqlSession().getMapper(Good.class).setPic2(id, img);
            SqlSessionUtil.getSqlSessionInstance().getSqlSession().commit();
        } else if (kind == 3) {
            SqlSessionUtil.getSqlSessionInstance().getSqlSession().getMapper(Good.class).setPic3(id, img);
            SqlSessionUtil.getSqlSessionInstance().getSqlSession().commit();
        } else {
            SqlSessionUtil.getSqlSessionInstance().getSqlSession().getMapper(Good.class).setPic4(id, img);
            SqlSessionUtil.getSqlSessionInstance().getSqlSession().commit();
        }
    }
}
